package com.ailot.cloud.base.common.dto;


import com.ailot.cloud.base.common.enums.CommonCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {

    /**
     * 消息code
     */
    private String code;

    /**
     * 消息
     */
    private String msg;

    /**
     * 返回数据
     */
    private T data;

    public Boolean isOk() {
        if (CommonCodeEnum.TRUE.getCode().equals(code)) {
            return true;
        } else {
            return false;
        }
    }

    public Result(String code) {
        this.code = code;
    }

    public static <T> Result<T> fail(String msg) {
        return new Result<>(CommonCodeEnum.FALSE.getCode(), msg, null);
    }

    public static <T> Result<T> fail(String msg, T data) {
        return new Result<>(CommonCodeEnum.FALSE.getCode(), msg, data);
    }

    public static <T> Result<T> fail() {
        return new Result<>(CommonCodeEnum.FALSE.getCode());
    }

    public static <T> Result<T> success(T data) {

        return new Result<>(CommonCodeEnum.TRUE.getCode(), null, data);
    }

    public static <T> Result<T> success(String msg, T data) {

        return new Result<>(CommonCodeEnum.TRUE.getCode(), msg, data);
    }

    public static <T> Result<T> success() {
        return new Result<>(CommonCodeEnum.TRUE.getCode());
    }


}
